﻿using Anke.SHManage.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Anke.SHManage.Web.Reports
{
    public partial class I_AccountExcelWeiXinImportWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnExportExcel_Click(object sender, EventArgs e)
        {
            //先上传到服务器 保存，然后再存入库
            if (this.FileUpload1.HasFile)
            {
                //this.lbl.Visible = true;
                //this.lbl.Text = "数据导入中,请稍等........";
                //this.BtnExcel2DataBase.Enabled = false; 
                try
                {
                    HttpPostedFile UpFile = this.FileUpload1.PostedFile;
                    int FileLength = UpFile.ContentLength;     //記錄文件長度
                    string fileNameWithoutExt = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
                    //扩展名  
                    string subCType = UpFile.ContentType.Substring(12, UpFile.ContentType.Length - 12);
                    string extentionName = "";
                    if (subCType == "x-excel" || subCType == "vnd.openxmlformats-officedocument.spreadsheetml.sheet") //EXCEL
                    {
                        extentionName = "xlsx";

                    }
                    else if (subCType == "vnd.ms-excel")
                    {
                        extentionName = "xls";
                    }
                    if (fileNameWithoutExt == "" || fileNameWithoutExt == null)
                    {
                        Response.Write("<script>alert('请先选择Excel文件！')</script>");
                        return;
                    }
                    if (extentionName != "xls" && extentionName != "xlsx")
                    {
                        Response.Write("<script>alert('您上传的不是Excel文件！')</script>");
                        return;
                    }

                    //string url = AppConfig.GetConfigString("EXCELImportIP");
                    //if (!Directory.Exists(url + "\\WeiXinPay"))
                    //{
                    //    try
                    //    {
                    //        Directory.CreateDirectory(url + "\\WeiXinPay");
                    //    }
                    //    catch
                    //    {
                    //        Response.Write("<script>alert('服务器创建目录出错！')</script>");
                    //        return;
                    //    }
                    //}
                    //this.FileUpload1.SaveAs(url + "\\WeiXinPay\\" + FileUpload1.FileName);

                    //Utility.ComExcelHelper comExcel = new Utility.ComExcelHelper(url + "\\WeiXinPay\\" + FileUpload1.FileName);
                    //comExcel.Load();
                    //字典表中通过顺序号来确定 去除头尾的行数
                    int cutHead = 5, cutBottom = 4;
                    cutHead = new TDictionaryBLL().GetDictionarySNByID("WeiXinExcelCut-1");
                    cutBottom = new TDictionaryBLL().GetDictionarySNByID("WeiXinExcelCut-2");

                    // Byte[] FileByteArray = new Byte[FileLength];   //文件臨時儲存Byte數組
                    // Stream StreamObject = UpFile.InputStream;      //建立數据流對像

                    //  StreamObject.Read(FileByteArray, 0, FileLength);
                    //  DataTable dt = Utility.ComExcelHelper.OpenWeiXinCSV(StreamObject, cutHead);
                    DataTable dt = Utility.ComExcelHelper.OpenWeiXinCSV(this.FileUpload1.PostedFile.InputStream, cutHead);
                    FileUpload1.PostedFile.InputStream.Close();
                    FileUpload1.PostedFile.InputStream.Dispose();

                    //DataTable dt = comExcel.ReadWeiXinPayExcel2DataTable(url + "\\WeiXinPay\\" + FileUpload1.FileName, "WeiXinPay", "商户订单号,创建时间,交易类型,交易流水号,订单金额,状态,备注", cutHead, cutBottom);
                    string result = "";
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        result = Utility.ComExcelHelper.Insert2Sql(dt);
                    }
                    else
                    {
                        //this.BtnExcel2DataBase.Enabled = true;
                        //this.lbl.Text = "";
                        Response.Write("<script>alert('EXCEL存入数据库失败：读取结果无可用数据！ ')</script>");
                        return;
                    }
                    //comExcel.Dispose();
                    if (result != "")
                    {
                        //this.BtnExcel2DataBase.Enabled = true;
                        //this.lbl.Text = "";
                        Response.Write("<script>alert('EXCEL存入数据库失败! ')</script>");
                        return;
                    }
                    if (result == "")
                    {
                        //this.BtnExcel2DataBase.Enabled = true;
                        //this.lbl.Text = "";
                        Response.Write("<script>alert('EXCEL存入数据库成功！')</script>");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    //this.BtnExcel2DataBase.Enabled = true;
                    //this.lbl.Text = "";
                    Response.Write("<script>alert('EXCEL存入数据库失败：" + ex.Message + " ')</script>");
                    return;
                }
            }
        }

    }
}